package com.ycxh.module.FileStore;

import com.ycxh.module.FileStore.entity.FileFolder;
import com.ycxh.module.FileStore.entity.FileInfo;
import com.ycxh.module.FileStore.entity.MyFile;
import com.ycxh.module.FileStore.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class FileStorePageController {

    // 磁盘根目录
    @Value("${file_manage.root_path:}")
    private String ROOT_PATH;

    /**
     * 去全部文件页
     *
     * @param fId   父文件id
     * @param path  文件路径
     * @param error 错误
     * @param map
     */
    // http://127.0.0.1:8088/deploy/files
    @GetMapping("/files")
    public String toFileStorePage(Integer fId, String path, Integer error, Map<String, Object> map) {
        // 判断是否包含错误信息
        // 错误信息会显示在标题之后
        if (error != null) {
            if (error == 1) {
                map.put("error", "添加失败！当前已存在同名文件夹");
            }
            if (error == 2) {
                map.put("error", "重命名失败！文件夹已存在");
            }
        }

        // 包含的子文件夹
        final List<FileFolder> folders = new ArrayList<>();
        // 包含的文件
        final List<MyFile> files = new ArrayList<>();
        // 当前文件夹信息
        final FileFolder nowFolder = new FileFolder();
        // 当前文件夹的相对路径
        List<FileFolder> location = new ArrayList<>();

        if (StringUtils.isBlank(ROOT_PATH)) {
            nowFolder.setFileFolderId(0);
            map.put("folders", folders);
            map.put("files", files);
            map.put("nowFolder", nowFolder);
            map.put("location", location);
            map.put("error", "未指定磁盘根目录，当前功能无法使用");
            return "file-manage/files";
        }

        if (StringUtils.isBlank(path)) {
            path = ROOT_PATH;
        } else if (fId != null && fId == -1) {
            // 返回上一级
            path = new File(path).getParentFile().getPath();
        }

        File file1 = new File(path);
        if (!file1.exists()) {
            nowFolder.setFileFolderId(0);
            map.put("folders", folders);
            map.put("files", files);
            map.put("nowFolder", nowFolder);
            map.put("location", location);
            map.put("error", "指定磁盘目录不存在，当前功能无法使用");
            return "file-manage/files";
        }

        List<FileInfo> fileList = FileUtils.getFiles(path);
        fileList.forEach(o -> {
            if (o.getType().endsWith("文件夹")) {
                FileFolder folder = FileFolder.builder().fileFolderName(o.getFileName())
                        .time(o.getUpdateDate()).path(o.getPath()).build();
                folders.add(folder);
            } else {
                MyFile myFile = MyFile.builder().myFileName(o.getFileName()).lSize(o.getLSize())
                        .uploadTime(o.getUpdateDate()).type(1).myFilePath(o.getPath()).build();
                files.add(myFile);
            }
        });

        if (ROOT_PATH.equals(path)) {
            // 根目录
            nowFolder.setFileFolderId(0); // 根目录
            nowFolder.setPath(path);
            location.add(nowFolder);
        } else {
            // 当前文件夹
            File file = new File(path);
            String fileName = file.getName();
            nowFolder.setFileFolderId(1);
            nowFolder.setFileFolderName(fileName);
            nowFolder.setPath(file.getPath());

            // 获取父级目录
            FileFolder temp = null;
            while (true) {
                file = file.getParentFile();

                if (file.getPath().equals(ROOT_PATH)) {
                    // 根目录
                    break;
                }
                fileName = file.getName();
                temp = new FileFolder();
                temp.setFileFolderName(fileName);
                temp.setPath(file.getPath());
                temp.setFileFolderId(1);
                location.add(temp);
            }
        }

        // 反转
        Collections.reverse(location);

        map.put("folders", folders);
        map.put("files", files);
        map.put("nowFolder", nowFolder);
        map.put("location", location);
        log.info("全部文件页面域中的数据:" + map);
        return "file-manage/files";
    }

}
